package view;
import java.awt.Component;
import java.awt.Graphics;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JLabel;
import controller.DragNDrop;
import javax.swing.border.*;
import javax.swing.JApplet;

/**
 * 
 */

/**
 * @author Leo COLLET, Nelle VAROQUAUX
 *
 */
public class Habit extends JPanel implements Icon, MouseListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static JLabel title;
	//private static ImageLabel icone;
	Border thickBorder = new LineBorder(Color.DARK_GRAY, 1);
	Border thickBorder_hover = new LineBorder(Color.DARK_GRAY, 2);

	public Habit(int x, int y, String label){
	    super();
	    this.buildWithTitle(x, y, label);
	}
	
	public Habit(int x, int y, String label, String path){
	    super();
	    this.buildWithIcon(x, y, path);
	}

	private void buildWithTitle(int x, int y, String label){
		//this.setLayout(null);
	    this.setBackground(Color.LIGHT_GRAY); 
	    this.setBorder(thickBorder);
	    this.setLocation(x, y);
	    //this.setBounds(x, y, 40, 40);
	    title = new JLabel(label);
	    this.add(title); // #TODO a remplacer par icone

	    addMouseListener(this);
	    this.setVisible(true);
	}
	
	private void buildWithIcon(int x, int y, String path){
		//this.setLayout(null);
	    this.setBackground(Color.LIGHT_GRAY); 
	    this.setBorder(thickBorder);
	    this.setLocation(x, y);
	    //this.setBounds(x, y, 40, 40);

	    ImageIcon iconimage = createImageIcon(path, "a pretty but meaningless splat");

	    JLabel icone = new JLabel(iconimage);
	    this.add(icone); // #TODO a remplacer par icone

	    addMouseListener(this);
	    this.setVisible(true);
	}



	/* (non-Javadoc)
	 * @see javax.swing.Icon#getIconHeight()
	 */
	@Override
	public int getIconHeight() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see javax.swing.Icon#getIconWidth()
	 */
	@Override
	public int getIconWidth() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics, int, int)
	 */
	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e){
	    DragNDrop dnd = DragNDrop.getInstance();
	    dnd.removeTarget(this);
	    System.out.println("Mouse entered in habit");
	}

	@Override
	public void mouseExited(MouseEvent e){
	    DragNDrop dnd = DragNDrop.getInstance();
	    dnd.removeTarget(this);
	    System.out.println("Mouse exited habit.");
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	    System.out.println("User clicked on Laundry  Basket");	
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		DragNDrop dnd = DragNDrop.getInstance();
		dnd.addSource(this);
		this.setBorder(thickBorder_hover);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		DragNDrop dnd = DragNDrop.getInstance();
		dnd.addSource(this);
		JComponent target = dnd.getTarget();
		this.setBorder(thickBorder);
		System.out.println("User released habit on" + target);	


	}

    protected ImageIcon createImageIcon(String path,
                                           String description) {
	java.net.URL imgURL = getClass().getResource(path);
	if (imgURL != null) {
	    return new ImageIcon(imgURL, description);
	} else {
	    System.err.println("Couldn't find file: " + path);
	    return null;
	}
    }
}
